package com.ffms.servlet;

import com.ffms.dao.impl.BankDaoImpl;
import com.ffms.dao.impl.MoneyDaoImpl;
import com.ffms.entity.Bank;
import com.ffms.entity.FamilyMember;
import com.ffms.entity.Money;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

@WebServlet("/UpdateMoneyServlet")
public class UpdateMoneyServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        //1.接收视图层传来的表单数据并封装成为对象
        String id = request.getParameter("id");
        if (id == null) {
            try {
                add(request, response);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            update(request, response, id);
        }

    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws ParseException, ServletException, IOException {
        String mid = request.getParameter("mid");
        String flag = request.getParameter("flag");
        String money = request.getParameter("money");
        String time = request.getParameter("time");
        BankDaoImpl bankDao = new BankDaoImpl();
        Bank bank = bankDao.getByMId(mid);
        int result = 0;
        if (flag.equals("0") || flag.equals("3")) {
            //存钱和取钱 0 存 1取 2借 3 还
                result = Integer.parseInt(bank.getBmoney()) + Integer.parseInt(money);
        } else if
             //取钱和还钱
        (flag.equals("1") || flag.equals("2")) {
            result = Integer.parseInt(bank.getBmoney()) - Integer.parseInt(money);
        }
        bank.setBmoney(result + "");
        bankDao.updateBank(bank);
        Money newMoney = new Money(flag, money, time, mid);
        MoneyDaoImpl moneyDao = new MoneyDaoImpl();
        int i = moneyDao.insertMoney(newMoney);
        int thisFlag = flag.equals("0") || (flag.equals("1")) ? 0 : 1;
        if (i > 0) {
            //成功则挑转到主页面
            request.getRequestDispatcher("/AllMoneyServlet?flag=" +thisFlag).forward(request, response);
        } else {
            //失败则提示
            request.setAttribute("error", "更新收入失败");
            FamilyMember user = (FamilyMember) request.getSession().getAttribute("user");
            request.getRequestDispatcher("/PreAddMoneyServlet?name=" + user.getName() + "&flag=" + thisFlag).forward(request, response);
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response, String id) throws ServletException, IOException {
        String aflag = request.getParameter("aflag");
        String flag = request.getParameter("flag");
        String money = request.getParameter("money");

        Money amoney = new Money(Integer.parseInt(id), flag, money);

        MoneyDaoImpl moneyDaoImpl = new MoneyDaoImpl();
        String flag1 = amoney.getFlag();

        if (flag1.contains("存钱")) {
            flag1 = "0";
        } else if (flag1.contains("取钱")) {
            flag1 = "1";
        } else if (flag1.contains("借钱")) {
            flag1 = "2";
        } else if (flag1.contains("还钱")) {
            flag1 = "3";
        }
        amoney.setFlag(flag1);
        int n = moneyDaoImpl.updateMoney(amoney);
        //3 根据结果跳转到不同页面
        if (n > 0) {
            //成功则挑转到主页面
            request.getRequestDispatcher("/AllMoneyServlet?flag=" + aflag).forward(request, response);
        } else {
            //失败则提示
            request.setAttribute("error", "更新收入失败");
            request.getRequestDispatcher("/PreUpdateMoneyServlet?id=" + id + "&flag=" + aflag).forward(request, response);
        }
    }
}
